var test;
$(document).click(function() {
    layer.close(test);
    $('a').removeClass('shadow')
});
$(document).on('click', '.btn-group', function(e) {
    e.stopPropagation()
});
var pageStart = 1;
var pageSize = 10;

function search() {
    var reach = document.getElementById("reach").value;
    if (reach != "") {
        $.ajax({
            url: Feng.ctxPath + "/customer/getCustomerByStore",
            type: "post",
            data: {
                reach: reach,
                pageStart: pageStart,
                pageSize: pageSize
            },
            success: function(data) {
                $("#dataBox").show();
                var patient = "";
                for (var i = 0; i < data.length; i++) {
                    patient += '<li style="padding: 5px 0 10px 10px;border-bottom: 1px solid #cccccc" id="' + data[i].id + '" class="' + data[i].id + '"><p><span>' + data[i].customerName + '</span>  <span>' + data[i].sex + '</span>  <span>' + data[i].customerAge + '</span>岁</p><p>' + data[i].customerPhone + '</p><p>' + data[i].arrivalStore + '</p></li>'
                };
                $("#dataUl").html(patient);
                if ((pageStart * pageSize) > data.length) {
                    $(".js-load-more").hide()
                } else {
                    $(".js-load-more").show()
                }
            },
            error: function(data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            }
        })
    } else {
        $("#dataBox").hide()
    }
}
$(document).on('click', '.js-load-more', function() {
    pageStart++;
    search()
});
$(document).click(function() {
    $("#dataBox").hide()
});
$(document).on('click', '.layui-layer', function(e) {
    e.stopPropagation()
});
$("#dataUl").on('click', 'li', function() {
    var reservationId = $(this).attr('class');
    $.ajax({
        url: Feng.ctxPath + "/consultantTwo/getdcsByReservationId",
        type: "post",
        data: {
            "id": reservationId
        },
        success: function(data) {
            if (data == 1) {
                $("#dataBox").hide();
                var index = layer.open({
                    type: 2,
                    title: '到店客户详情',
                    area: ['100%', '100%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/customerDetails/to_openBileLists/' + reservationId + "/0"
                });
                this.layerIndex = index
            } else {
                alert("请先联系前台客服匹配建档！");
                return
            }
        }
    })
});
appointment = function(customerId, reservationsId, doctor) {
    layer.close(test);
    var cid = customerId + "," + reservationsId + "," + doctor;
    var index = layer.open({
        type: 2,
        title: '新建预约',
        area: ['70%', '95%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/doctor_appointment/' + cid
    });
    this.layerIndex = index
};
shortMessage = function(customerId) {
    layer.close(test);
    var index = layer.open({
        type: 2,
        title: '发送消息',
        area: ['60%', '90%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/msgSend/to_msgSend/' + customerId
    });
    this.layerIndex = index
};
openBilee = function(ide) {
    $.ajax({
        url: Feng.ctxPath + "/consultantTwo/getdcsByCustomerId",
        type: "post",
        data: {
            "id": ide
        },
        success: function(data) {
            if (data == 1) {
                layer.close(test);
                var cd = $(".actionClick", parent.document).children('p').html();
                if (cd == '到店') {
                    var index = layer.open({
                        type: 2,
                        title: '到店客户详情',
                        area: ['100%', '100%'],
                        fix: true,
                        maxmin: true,
                        content: Feng.ctxPath + '/customerDetails/to_openBileLists/' + ide + "/0"
                    });
                    this.layerIndex = index
                } else if (cd == 'TC') {
                    var index = layer.open({
                        type: 2,
                        title: '患者信息',
                        area: ['100%', '100%'],
                        fix: true,
                        maxmin: true,
                        content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + ide + '/0/0'
                    });
                    this.layerIndex = index
                } else if (cd == '医生') {
                    var index = layer.open({
                        type: 2,
                        title: '患者信息',
                        area: ['100%', '100%'],
                        fix: true,
                        maxmin: true,
                        content: Feng.ctxPath + '/doctorDetails/doctor_seeDiagnosis/' + ide + '/' + 0
                    });
                    this.layerIndex = index
                }
            } else {
                alert("请先联系前台客服匹配建档！");
                return
            }
        }
    })
};
takeOne = function() {
    $(".layui-layer-content .ulBox,.layui-layer-content .btnBox").show();
    $("#bookingBox,#takeOne").hide()
};
getMakeNumer = function(reservationsId) {
    $.ajax({
        url: Feng.ctxPath + "/customer/getMakeNumer",
        type: "post",
        data: {
            reservationsId: reservationsId
        },
        success: function(data) {
            var before = '<div style="font-weight: bolder;font-size: 14px;padding: 5px 0 1px;border-bottom: 1px solid #DDDDDD;color: black;">之后预约</div>';
            var today = '<div style="font-weight: bolder;font-size: 14px;padding: 0 0 1px;border-bottom: 1px solid #DDDDDD;color: black;">当日预约</div>';
            var after = '<div style="font-weight: bolder;font-size: 14px;padding: 0 0 1px;border-bottom: 1px solid #DDDDDD;color: black;">之前预约</div>';
            for (var i = 0; i < data.length; i++) {
                if (data[i].name == "今天") {
                    today += '<div><span style="font-weight: bold;">' + data[i].yearday + '</span><span>' + data[i].startTime + '</span>-<span>' + data[i].endTime + ' </span><span>' + data[i].doctor + ' </span><span>' + data[i].appointmentPayService + '</span></div>'
                } else if (data[i].name == "之后") {
                    before += '<div><span style="font-weight: bold;">' + data[i].yearday + '</span><span>' + data[i].startTime + '</span>-<span>' + data[i].endTime + ' </span><span>' + data[i].doctor + ' </span><span>' + data[i].appointmentPayService + '</span></div>'
                } else if (data[i].name == "之前") {
                    after += '<div><span style="font-weight: bold;">' + data[i].yearday + '</span><span>' + data[i].startTime + '</span>-<span>' + data[i].endTime + ' </span><span>' + data[i].doctor + ' </span><span>' + data[i].appointmentPayService + '</span></div>'
                }
            }
            $(".layui-layer-content .ulBox,.layui-layer-content .btnBox").hide();
            $("#bookingBox").html(today + before + after).show();
            $("#takeOne").show()
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
modifyAppointment = function(ids) {
    $.ajax({
        url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + ids,
        type: "post",
        success: function(data) {
            if (data == 1) {
                alert("此患者已开单不能修改预约时间！");
                return
            } else if (data == 4) {
                alert("此患者充了储值或积分不能修改预约时间！");
                return
            } else {
                layer.close(test);
                var index = layer.open({
                    type: 2,
                    title: '修改预约客户',
                    area: ['70%', '95%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/doctorLooked/doctor_addTasks/' + ids
                });
                this.layerIndex = index
            }
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
cancelAppointment = function(customerId, customerName, calEvent) {
    $.ajax({
        url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + customerId,
        type: "post",
        success: function(data) {
            if (data == 1) {
                alert("此患者已开单不能取消预约！");
                return
            } else if (data == 4) {
                alert("此患者充了储值或积分不能取消预约！");
                return
            } else {
                layer.close(test);
                var index = layer.open({
                    type: 2,
                    title: '取消预约',
                    area: ['30%', '40%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/doctorLooked/cancelAppointment/' + customerId
                });
                this.layerIndex = index
            }
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
registerAppointment = function(customerId, customerName, calEvent) {
    if (confirm("确定对患者【" + customerName + "】挂号?")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/regresses",
            type: "post",
            data: {
                customerId: customerId
            },
            success: function(data) {
                if (data == 1) {
                    layer.close(test);
                    Feng.success('挂号失败!患者' + customerName + '预约时间不是今天！');
                    $('#calendar').fullCalendar('updateEvent', calEvent)
                } else {
                    layer.close(test);
                    Feng.success('患者' + customerName + '挂号成功!');
                    window.$('#calendar').fullCalendar('refetchEvents')
                }
            },
            error: function(data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            }
        })
    }
};
temporaryRegister = function(customerId, customerName, calEvent) {
    var appointmentTime = calEvent.start.format('YYYY-MM-DD');
    var nowTime = setDateTime(new Date(), 0);
    if (appointmentTime > nowTime) {
        layer.close(test);
        Feng.success('挂号失败!患者' + customerName + '预约时间不是今天！');
        $('#calendar').fullCalendar('updateEvent', calEvent)
    } else {
        if (confirm("患者【" + customerName + "】为临时患者，需完善信息才能挂号")) {
            layer.close(test);
            var index = layer.open({
                type: 2,
                title: '添加到店客户',
                area: ['90%', '90%'],
                fix: true,
                maxmin: true,
                content: Feng.ctxPath + '/customer/getTemporaryCustomer/' + customerId
            });
            this.layerIndex = index
        }
    }
};
noSureAppointment = function(customerId, customerName, calEvent) {
    $.ajax({
        url: Feng.ctxPath + "/doctorLooked/no_sureAppointment",
        type: "post",
        data: {
            customerId: customerId
        },
        success: function(data) {
            layer.close(test);
            calEvent.arrivalStatus = "1";
            calEvent.backgroundColor = "#4099ff";
            calEvent.borderColor = "#4099ff";
            $('#calendar').fullCalendar('updateEvent', calEvent)
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
sureAppointment = function(customerId, customerName, calEvent) {
    $.ajax({
        url: Feng.ctxPath + "/doctorLooked/sureAppointment",
        type: "post",
        data: {
            customerId: customerId
        },
        success: function(data) {
            layer.close(test);
            calEvent.arrivalStatus = "6";
            calEvent.backgroundColor = "#017cdc";
            calEvent.borderColor = "#017cdc";
            $('#calendar').fullCalendar('updateEvent', calEvent)
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
sureAppointments = function(customerId, customerName, calEvent) {
    $.ajax({
        url: Feng.ctxPath + "/doctorLooked/sureAppointment",
        type: "post",
        data: {
            customerId: customerId
        },
        success: function(data) {
            layer.close(test);
            calEvent.arrivalStatus = "10";
            calEvent.backgroundColor = "#017cdc";
            calEvent.borderColor = "#017cdc";
            $('#calendar').fullCalendar('updateEvent', calEvent)
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
startTreatment = function(customerId, customerName, calEvent) {
    if (confirm("确定患者【" + customerName + "】已经开始治疗?")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/startTreatment",
            type: "post",
            data: {
                customerId: customerId
            },
            success: function(data) {
                layer.close(test);
                Feng.success('患者' + customerName + '开始治疗!');
                calEvent.arrivalStatus = "3";
                calEvent.backgroundColor = "#ffba40";
                calEvent.borderColor = "#ffba40";
                $('#calendar').fullCalendar('updateEvent', calEvent)
            },
            error: function(data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            }
        })
    }
};
completionTreatment = function(customerId, customerName, calEvent) {
    if (confirm("确定患者【" + customerName + "】已经完成治疗?")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/completionTreatment",
            type: "post",
            data: {
                customerId: customerId
            },
            success: function(data) {
                layer.close(test);
                Feng.success('患者' + customerName + '完成治疗!');
                calEvent.arrivalStatus = "4";
                calEvent.backgroundColor = "#2dc87e";
                calEvent.borderColor = "#2dc87e";
                $('#calendar').fullCalendar('updateEvent', calEvent)
            },
            error: function(data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            }
        })
    }
};
alreadyLeave = function(customerId, customerName, calEvent) {
    if (confirm("确定患者【" + customerName + "】已经离开?")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/alreadyLeave",
            type: "post",
            data: {
                customerId: customerId
            },
            success: function(data) {
                layer.close(test);
                Feng.success('患者' + customerName + '已经离开!');
                calEvent.arrivalStatus = "5";
                calEvent.backgroundColor = "#61b740";
                calEvent.borderColor = "#61b740";
                $('#calendar').fullCalendar('updateEvent', calEvent)
            },
            error: function(data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            }
        })
    }
};
fallbackAction = function(customerId, customerName, arrivalStatus, calEvent) {
    $.ajax({
        url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + customerId,
        type: "post",
        success: function(data) {
            if (calEvent.arrivalStatus == "2") {
                if (data == 1) {
                    alert("此患者已开单不能回退！");
                    return
                } else if (data == 3) {
                    alert("此患者初诊已挂号不能回退！");
                    return
                } else if (data == 4) {
                    alert("此患者充了储值或积分不能回退！");
                    return
                } else {
                    if (confirm("确定要回退么？ 患者【" + customerName + "】")) {
                        $.ajax({
                            url: Feng.ctxPath + "/doctorLooked/fallbackAction",
                            type: "post",
                            data: {
                                customerId: customerId,
                                arrivalStatus: arrivalStatus
                            },
                            success: function(data) {
                                layer.close(test);
                                Feng.success('患者' + customerName + '回退成功!');
                                if (calEvent.arrivalStatus == "2") {
                                    calEvent.arrivalStatus = "1";
                                    calEvent.backgroundColor = "#4bbcff";
                                    calEvent.borderColor = "#4bbcff"
                                } else if (calEvent.arrivalStatus == "3") {
                                    calEvent.arrivalStatus = "2";
                                    calEvent.backgroundColor = "#10428b";
                                    calEvent.borderColor = "#10428b"
                                } else if (calEvent.arrivalStatus == "4") {
                                    calEvent.arrivalStatus = "3";
                                    calEvent.backgroundColor = "#ffba40";
                                    calEvent.borderColor = "#ffba40"
                                } else if (calEvent.arrivalStatus == "5") {
                                    calEvent.arrivalStatus = "4";
                                    calEvent.backgroundColor = "#2dc87e";
                                    calEvent.borderColor = "#2dc87e"
                                }
                                $('#calendar').fullCalendar('updateEvent', calEvent)
                            },
                            error: function(data) {
                                alert('没有权限，请联系系统管理员!');
                                parent.layer.close(parent.layer.getFrameIndex(window.name))
                            }
                        })
                    }
                }
            } else {
                if (confirm("确定要回退么？ 患者【" + customerName + "】")) {
                    $.ajax({
                        url: Feng.ctxPath + "/doctorLooked/fallbackAction",
                        type: "post",
                        data: {
                            customerId: customerId,
                            arrivalStatus: arrivalStatus
                        },
                        success: function(data) {
                            layer.close(test);
                            Feng.success('患者' + customerName + '回退成功!');
                            if (calEvent.arrivalStatus == "2") {
                                calEvent.arrivalStatus = "1";
                                calEvent.backgroundColor = "#4bbcff";
                                calEvent.borderColor = "#4bbcff"
                            } else if (calEvent.arrivalStatus == "3") {
                                calEvent.arrivalStatus = "2";
                                calEvent.backgroundColor = "#10428b";
                                calEvent.borderColor = "#10428b"
                            } else if (calEvent.arrivalStatus == "4") {
                                calEvent.arrivalStatus = "3";
                                calEvent.backgroundColor = "#ffba40";
                                calEvent.borderColor = "#ffba40"
                            } else if (calEvent.arrivalStatus == "5") {
                                calEvent.arrivalStatus = "4";
                                calEvent.backgroundColor = "#2dc87e";
                                calEvent.borderColor = "#2dc87e"
                            }
                            $('#calendar').fullCalendar('updateEvent', calEvent)
                        },
                        error: function(data) {
                            alert('没有权限，请联系系统管理员!');
                            parent.layer.close(parent.layer.getFrameIndex(window.name))
                        }
                    })
                }
            }
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
printingAppointment = function(customerId) {
    layer.close(test);
    var index = layer.open({
        type: 2,
        title: '打印客户信息',
        area: ['60%', '100%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/printingAppointment/' + customerId
    });
    this.layerIndex = index
};
toReconsulting = function(id) {
    layer.close(test);
    var index = layer.open({
        type: 2,
        title: '患者重咨',
        area: ['35%', '50%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/customer/to_reconsulting/' + id
    });
    this.layerIndex = index
};
btnRefetchEvents = function() {
    $('#calendar').fullCalendar('refetchEvents')
};
numberedModification = function(customerId, yellowNumber) {
    layer.close(test);
    var yi = yellowNumber + "," + customerId;
    var index = layer.open({
        type: 2,
        title: '修改黄页编号',
        area: ['30%', '40%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/numberedModification/' + yi
    });
    this.layerIndex = index
};
getUpdateLog = function(customerId) {
    var index = layer.open({
        type: 2,
        title: '改动日志',
        area: ['80%', '80%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/getUpdateLog/' + customerId
    });
    this.layerIndex = index
};
updAppointmentPayService = function(customerId) {
    layer.close(test);
    var index = layer.open({
        type: 2,
        title: '修改看诊项目',
        area: ['50%', '50%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/updAppointmentPayService/' + customerId
    });
    this.layerIndex = index
};
updRemarksReservation = function(customerId) {
    layer.close(test);
    var index = layer.open({
        type: 2,
        title: '修改预约备注',
        area: ['30%', '40%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/updRemarksReservation/' + customerId
    });
    this.layerIndex = index
};
choiceTc = function(id) {
    layer.close(test);
    $.ajax({
        url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + id,
        type: "post",
        success: function(data) {
            if (data == 1) {
                alert("此患者已开单不能修改TC！");
                return
            } else {
                var index = layer.open({
                    type: 2,
                    title: '选择Tc',
                    area: ['50%', '60%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/customer/to_choiceTc/' + id
                });
                this.layerIndex = index
            }
        },
        error: function(data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};